using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class WeaponHangController : MonoBehaviour
{
    private WeaponHangSlot[] _weaponHangSlotList;

    private List<GameObject> _weaponPrefabList = new();
    
    private void Awake()
    {
        _weaponHangSlotList = GetComponentsInChildren<WeaponHangSlot>();
    }

    private void OnEnable()
    {
        EventCenter.AddEventListener<Dictionary<int, ItemDataSO>>(EventName.UpdateHangedWepaon, UpdatePlayerWeaponHang);
    }

    private void OnDisable()
    {
        EventCenter.RemoveEventListener<Dictionary<int, ItemDataSO>>(EventName.UpdateHangedWepaon, UpdatePlayerWeaponHang);
    }
    
    private void UpdatePlayerWeaponHang(Dictionary<int, ItemDataSO> equipedWeaponDict)
    {
        ResetAllHangedWeapon();

        int weaponCount = GetAllWeaponPrefab(equipedWeaponDict);
        if (weaponCount == 0)
            return;

        else if (weaponCount == 1)
        {
            WeaponHangSlot weaponHangSlot = GetHangPoint(WepaonHangPoint.Center);
            weaponHangSlot.HandleWeaponHang(_weaponPrefabList[0]);
        }
        else if (weaponCount == 2)
        {
            WeaponHangSlot weaponHangSlot = GetHangPoint(WepaonHangPoint.Left);
            weaponHangSlot.HandleWeaponHang(_weaponPrefabList[0]);

            weaponHangSlot = GetHangPoint(WepaonHangPoint.Right);
            weaponHangSlot.HandleWeaponHang(_weaponPrefabList[1]);
        }
        else if (weaponCount == 3)
        {
            WeaponHangSlot weaponHangSlot = GetHangPoint(WepaonHangPoint.Left);
            weaponHangSlot.HandleWeaponHang(_weaponPrefabList[0]);

            weaponHangSlot = GetHangPoint(WepaonHangPoint.Right);
            weaponHangSlot.HandleWeaponHang(_weaponPrefabList[1]);

            weaponHangSlot = GetHangPoint(WepaonHangPoint.Center);
            weaponHangSlot.HandleWeaponHang(_weaponPrefabList[2]);
        }
        else if(weaponCount == 4)
        {
            WeaponHangSlot weaponHangSlot = GetHangPoint(WepaonHangPoint.LeftUp);
            weaponHangSlot.HandleWeaponHang(_weaponPrefabList[0]);

            weaponHangSlot = GetHangPoint(WepaonHangPoint.RightUp);
            weaponHangSlot.HandleWeaponHang(_weaponPrefabList[1]);

            weaponHangSlot = GetHangPoint(WepaonHangPoint.LeftDown);
            weaponHangSlot.HandleWeaponHang(_weaponPrefabList[2]);

            weaponHangSlot = GetHangPoint(WepaonHangPoint.RightDown);
            weaponHangSlot.HandleWeaponHang(_weaponPrefabList[3]);
        }
    }
    private WeaponHangSlot GetHangPoint(WepaonHangPoint wepaonHangPoint)
    {
        WeaponHangSlot pointSlot = _weaponHangSlotList?.Where(t => t.wepaonHangPoint == wepaonHangPoint)
                                                  ?.Select(t => t.GetComponent<WeaponHangSlot>())
                                                  ?.FirstOrDefault();

        return pointSlot;
    }
    private int GetAllWeaponPrefab(Dictionary<int, ItemDataSO> equipedWeaponDict)
    {
        _weaponPrefabList.Clear();
        foreach (var equipment in equipedWeaponDict)
        {
            if (equipment.Value is WeaponScrollDataSO weaponItemDataSO)
                _weaponPrefabList.Add(weaponItemDataSO.weaponPrefab);
            
        }
        return _weaponPrefabList.Count;
    }
    public void ResetAllHangedWeapon()
    {
        foreach (var weaponSlot in _weaponHangSlotList)
            weaponSlot.DestoryWeapon();
    }
}
